﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System;
using System.Dynamic;
using System.Linq.Expressions;
using Microsoft.Scripting.Actions.Calls;
using Microsoft.Scripting.Utils;

namespace Cjc.Calculation.ExpressionEngine.Excel.Runtime.Binding
{

    /// <summary>
    /// ArgBuilder which provides the WorksheetScope parameter to a method.
    /// </summary>
    public sealed class ScopeArgBuilder : ArgBuilder
    {
        private static Func<object[], object> _readFunc = (Func<object[], object>)Delegate.CreateDelegate(typeof(Func<object[], object>), 0, typeof(ArgBuilder).GetMethod("ArgumentRead"));
        public ScopeArgBuilder(ParameterInfo info)
            : base(info)
        {
        }

        public override int Priority
        {
            get { return -1; }
        }

        public override int ConsumedArgumentCount
        {
            get { return 0; }
        }

        protected override Expression ToExpression(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed)
        {
            return ((ExcelOverloadResolver)resolver).ScopeExpression;
        }

        protected override Func<object[], object> ToDelegate(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed)
        {
            return _readFunc;
        }
    }
}
